class GfpService {

    //FIXME true or false?
    boolean transactional = false
    def geoService

    /**
    * mark whether eventdata should be geo-fence reported!
    */
    def markGfp(ed, device) {

        if (!ed.gfpType) {
            ed.gfpType = " "
        }

        def group = device.devicegroup

        if (group.toNotifyOverspeed) {
            if (ed.speed > group.maxSpeed) {
                ed.gfp = true
                ed.gfpType += "speed;"
            } else {
                println "overspeed no"
            }
        }

        if (group.toNotifyDormantOverMaxTime) {
            println "checking dormant"
            if (ed.speed < group.minSpeed) {
                def dormant = false

                //FIXME time-range
                def eds = Eventdata.findAllByDeviceAndDateCreatedBetween(device, new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() - group.maxDormantTime * 1000))
                if (eds.size() > 0) {
                    dormant = true
                    eds.each {
                        fed ->
                        if (fed.speed > group.minSpeed) {
                            dormant = false
                        }
                    }
                }

                if (dormant == true) {
                    ed.gfp = true
                    ed.gfpType += "dormant;"
                }
            }
        }

        if (group.geozones) {

            def zones = group.geozones
            def lasted = Eventdata.findByDevice(device, [max: 1, sort: "dateCreated", order: "desc"])



            if (lasted) {
                zones.each {
                    zone ->
                    if (zone.zoneType == "Rectangle") {
                        def first
                        def last
                        zone.points.each {
                            point ->
                            if (!first) {
                                first = point
                            } else {
                                last = point;
                            }
                        }

                        def one = geoService.inRect(ed.latitude, ed.longitude, last.latitude, first.latitude, last.longitude, first.longitude)
                        def two = geoService.inRect(lasted.latitude, lasted.longitude, last.latitude, first.latitude, last.longitude, first.longitude)

                        if (one != two) {
                            ed.gfp = true
                            ed.gfpType += "geozoneRectangle," + zone.id + ";"
                        }
                    } else if (zone.zoneType == "Circle") {
                        //FIXME todo
                        println "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCcccc begin";

                       def first
                       def last

                       zone.points.each {
                            point ->
                            if (!first) {
                                first = point
                            } else {
                                last = point;
                            }
                        }

                        def center = first.center ? first : last;
                        def circum = first.center ? last : first;

                        def lat = center.latitude
                        def lon = center.longitude

                        def d = geoService.getDistance(center, circum)
                        def d1 = geoService.getDistance(ed.latitude, ed.longitude, lat, lon)
                        def d2 = geoService.getDistance(lasted.latitude, lasted.longitude, lat, lon)

                        if ((d1 - d) * (d2 - d) < 0) {
                            ed.gfp = true
                            ed.gfpType += zone.geozoneNotifyType + " geozoneCircle" + zone.id + ";"
                        }
                    }
                    else if (zone.zoneType == "Triangle" || zone.zoneType == "Polygon") {
                        if (zone.points?.size()) {
                            def points = []
                            def first;
                            zone.points.each {
                                point ->
                                println point
                                points.add(point)
                                if (!first) {
                                    first = point
                                }
                            }
                            points.add(first);

                            def one = geoService.inPolygon(["latitude": ed.latitude, "longitude": ed.longitude], points)
                            def two = geoService.inPolygon(["latitude": lasted.latitude, "longitude": lasted.longitude], points)

                            if (one != two) {
                                ed.gfp = true
                                ed.gfpType += "geozone" + zone.zoneType+ " ," + zone.id + ";"
                            }
                        }
                        println "TTTTTTTTTTTTTTTTTTTTTTTTTTTT end";
                    }

                }
            }
        }
    }
}

